﻿using System;
using System.Data;
using System.Collections.Generic;
using XinYiOffice.Common;
using XinYiOffice.Model;
namespace XinYiOffice.BLL
{
	/// <summary>
	/// 销售机会
	/// </summary>
	public partial class SalesOpportunities
	{
		private readonly XinYiOffice.DAL.SalesOpportunities dal=new XinYiOffice.DAL.SalesOpportunities();
		public SalesOpportunities()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
			return dal.GetMaxId();
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int Id)
		{
			return dal.Exists(Id);
		}

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int  Add(XinYiOffice.Model.SalesOpportunities model)
		{
			return dal.Add(model);
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(XinYiOffice.Model.SalesOpportunities model)
		{
			return dal.Update(model);
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int Id)
		{
			
			return dal.Delete(Id);
		}
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool DeleteList(string Idlist )
		{
			return dal.DeleteList(Idlist );
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public XinYiOffice.Model.SalesOpportunities GetModel(int Id)
		{
			
			return dal.GetModel(Id);
		}

		/// <summary>
		/// 得到一个对象实体，从缓存中
		/// </summary>
		public XinYiOffice.Model.SalesOpportunities GetModelByCache(int Id)
		{
			
			string CacheKey = "SalesOpportunitiesModel-" + Id;
			object objModel = XinYiOffice.Common.DataCache.GetCache(CacheKey);
			if (objModel == null)
			{
				try
				{
					objModel = dal.GetModel(Id);
					if (objModel != null)
					{
						int ModelCache = XinYiOffice.Common.ConfigHelper.GetConfigInt("ModelCache");
						XinYiOffice.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
					}
				}
				catch{}
			}
			return (XinYiOffice.Model.SalesOpportunities)objModel;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			return dal.GetList(strWhere);
		}
		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			return dal.GetList(Top,strWhere,filedOrder);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<XinYiOffice.Model.SalesOpportunities> GetModelList(string strWhere)
		{
			DataSet ds = dal.GetList(strWhere);
			return DataTableToList(ds.Tables[0]);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<XinYiOffice.Model.SalesOpportunities> DataTableToList(DataTable dt)
		{
			List<XinYiOffice.Model.SalesOpportunities> modelList = new List<XinYiOffice.Model.SalesOpportunities>();
			int rowsCount = dt.Rows.Count;
			if (rowsCount > 0)
			{
				XinYiOffice.Model.SalesOpportunities model;
				for (int n = 0; n < rowsCount; n++)
				{
					model = new XinYiOffice.Model.SalesOpportunities();
					if(dt.Rows[n]["Id"]!=null && dt.Rows[n]["Id"].ToString()!="")
					{
						model.Id=int.Parse(dt.Rows[n]["Id"].ToString());
					}
					if(dt.Rows[n]["OppName"]!=null && dt.Rows[n]["OppName"].ToString()!="")
					{
					model.OppName=dt.Rows[n]["OppName"].ToString();
					}
					if(dt.Rows[n]["CustomerService"]!=null && dt.Rows[n]["CustomerService"].ToString()!="")
					{
						model.CustomerService=int.Parse(dt.Rows[n]["CustomerService"].ToString());
					}
					if(dt.Rows[n]["PersonalAccountId"]!=null && dt.Rows[n]["PersonalAccountId"].ToString()!="")
					{
						model.PersonalAccountId=int.Parse(dt.Rows[n]["PersonalAccountId"].ToString());
					}
					if(dt.Rows[n]["MarketingPlanId"]!=null && dt.Rows[n]["MarketingPlanId"].ToString()!="")
					{
						model.MarketingPlanId=int.Parse(dt.Rows[n]["MarketingPlanId"].ToString());
					}
					if(dt.Rows[n]["Source"]!=null && dt.Rows[n]["Source"].ToString()!="")
					{
						model.Source=int.Parse(dt.Rows[n]["Source"].ToString());
					}
					if(dt.Rows[n]["Requirement"]!=null && dt.Rows[n]["Requirement"].ToString()!="")
					{
					model.Requirement=dt.Rows[n]["Requirement"].ToString();
					}
					if(dt.Rows[n]["EstAmount"]!=null && dt.Rows[n]["EstAmount"].ToString()!="")
					{
						model.EstAmount=decimal.Parse(dt.Rows[n]["EstAmount"].ToString());
					}
					if(dt.Rows[n]["CurrencyNotes"]!=null && dt.Rows[n]["CurrencyNotes"].ToString()!="")
					{
					model.CurrencyNotes=dt.Rows[n]["CurrencyNotes"].ToString();
					}
					if(dt.Rows[n]["Feasibility"]!=null && dt.Rows[n]["Feasibility"].ToString()!="")
					{
						model.Feasibility=int.Parse(dt.Rows[n]["Feasibility"].ToString());
					}
					if(dt.Rows[n]["PresentStage"]!=null && dt.Rows[n]["PresentStage"].ToString()!="")
					{
						model.PresentStage=int.Parse(dt.Rows[n]["PresentStage"].ToString());
					}
					if(dt.Rows[n]["NextStep"]!=null && dt.Rows[n]["NextStep"].ToString()!="")
					{
						model.NextStep=int.Parse(dt.Rows[n]["NextStep"].ToString());
					}
					if(dt.Rows[n]["Sate"]!=null && dt.Rows[n]["Sate"].ToString()!="")
					{
						model.Sate=int.Parse(dt.Rows[n]["Sate"].ToString());
					}
					if(dt.Rows[n]["StageRemarks"]!=null && dt.Rows[n]["StageRemarks"].ToString()!="")
					{
					model.StageRemarks=dt.Rows[n]["StageRemarks"].ToString();
					}
					if(dt.Rows[n]["OccTime"]!=null && dt.Rows[n]["OccTime"].ToString()!="")
					{
						model.OccTime=DateTime.Parse(dt.Rows[n]["OccTime"].ToString());
					}
					if(dt.Rows[n]["EstDate"]!=null && dt.Rows[n]["EstDate"].ToString()!="")
					{
						model.EstDate=DateTime.Parse(dt.Rows[n]["EstDate"].ToString());
					}
					if(dt.Rows[n]["RefreshAccountId"]!=null && dt.Rows[n]["RefreshAccountId"].ToString()!="")
					{
						model.RefreshAccountId=int.Parse(dt.Rows[n]["RefreshAccountId"].ToString());
					}
					if(dt.Rows[n]["CreateAccountId"]!=null && dt.Rows[n]["CreateAccountId"].ToString()!="")
					{
						model.CreateAccountId=int.Parse(dt.Rows[n]["CreateAccountId"].ToString());
					}
					if(dt.Rows[n]["CreateTime"]!=null && dt.Rows[n]["CreateTime"].ToString()!="")
					{
						model.CreateTime=DateTime.Parse(dt.Rows[n]["CreateTime"].ToString());
					}
					if(dt.Rows[n]["RefreshTime"]!=null && dt.Rows[n]["RefreshTime"].ToString()!="")
					{
						model.RefreshTime=DateTime.Parse(dt.Rows[n]["RefreshTime"].ToString());
					}
					if(dt.Rows[n]["TenantId"]!=null && dt.Rows[n]["TenantId"].ToString()!="")
					{
						model.TenantId=int.Parse(dt.Rows[n]["TenantId"].ToString());
					}
					modelList.Add(model);
				}
			}
			return modelList;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetAllList()
		{
			return GetList("");
		}

		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			return dal.GetRecordCount(strWhere);
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			return dal.GetListByPage( strWhere,  orderby,  startIndex,  endIndex);
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		//public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		//{
			//return dal.GetList(PageSize,PageIndex,strWhere);
		//}

		#endregion  Method
	}
}

